package com.redoor.app.stake.auth.service.impl;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.auth.dao.BaseMenuMapper;
import com.redoor.app.stake.auth.dao.BaseRoleMapper;
import com.redoor.app.stake.auth.dao.BaseRoleMenuMapper;
import com.redoor.app.stake.auth.model.BaseRole;
import com.redoor.app.stake.auth.model.BaseRoleCriteria;
import com.redoor.app.stake.auth.model.BaseRoleMenu;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.model.controllerModel.BaseRoleActionBean;
import com.redoor.app.stake.auth.service.BaseRoleService;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by wangjunlei on 2018-02-07 15:19:13.
 */
@Service
public class BaseRoleServiceImpl extends AbstractService<BaseRole, String> implements BaseRoleService {

    @Autowired
    private BaseRoleMapper baseRoleMapper;
    @Autowired
    private BaseRoleMenuMapper baseRoleMenuMapper;
    @Autowired
    private BaseUserService baseUserService;

    @Override
    public List<BaseRole> qryRole(BaseRoleCriteria baseRoleCriteria) {
        //查询角色列表
        BaseRole role = new BaseRole();
        role.setRolename(baseRoleCriteria.getRoleName());
        role.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        //查询登录用户是否有超级管理员权限
        List<BaseRole> roles = this.qryUserRoleByUuid(this.baseUserService.getShiroUserUuid());
        for (BaseRole bean: roles ) {
            if("3".equals(bean.getUuid())){
                role.setUuid("3");
            }
        }
        List<BaseRole> list = this.baseRoleMapper.qryRole(role);
        return list;
    }

    @Override
    public List<BaseRole> qryUserRoleByUuid(String uuid) {
        List<BaseRole> list = this.baseRoleMapper.qryUserRoleByUuid(uuid);
        return list;
    }

    @Override
    public void insertRole(BaseRoleCriteria baseRoleCriteria) {
        BaseRoleActionBean baseRoleAction = new BaseRoleActionBean();
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        baseRoleAction.setCreatedat(new Date());
        baseRoleAction.setCreatoruuid(loginUser.getUuid());
        baseRoleAction.setUuid(UUID.randomUUID().toString());
        baseRoleAction.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        baseRoleAction.setMenuUuids(baseRoleCriteria.getMenuUuids());
        baseRoleAction.setRolename(baseRoleCriteria.getRoleName());
        this.baseRoleMapper.insert(baseRoleAction);
        //新增角色对应菜单
        this.insertRoleMenu(baseRoleAction);
    }

    @Override
    public void updateByStatus(BaseRole baseRole) {
        this.baseRoleMapper.updateByPrimaryKeySelective(baseRole);
        BaseRoleMenu baseRoleMenu = new BaseRoleMenu();
        baseRoleMenu.setRoleuuid(baseRole.getUuid());
        this.baseRoleMenuMapper.delete(baseRoleMenu);
    }

    @Override
    public void updateByUUid(BaseRoleCriteria baseRoleCriteria) {
        BaseRoleActionBean baseRoleAction =  new BaseRoleActionBean();
        //获取登录信息
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        baseRoleAction.setUpdateruuid(loginUser.getUuid());
        baseRoleAction.setUpdatedat(new Date());
        baseRoleAction.setUuid(baseRoleCriteria.getUuid());
        baseRoleAction.setRolename(baseRoleCriteria.getRoleName());
        baseRoleAction.setMenuUuids(baseRoleCriteria.getMenuUuids());
        //根据主键修改角色数据
        this.baseRoleMapper.updateByPrimaryKeySelective(baseRoleAction);
        BaseRoleMenu baseRoleMenu = new BaseRoleMenu();
        baseRoleMenu.setRoleuuid(baseRoleCriteria.getUuid());
        //清空角色对应的所有菜单
        this.baseRoleMenuMapper.delete(baseRoleMenu);
        //添加角色新增菜单
        this.insertRoleMenu(baseRoleAction);
    }

    @Override
    public List<BaseRole> findRoleAll() {
        BaseRole baseRole = new BaseRole();
        baseRole.setStatus(Integer.parseInt(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        return this.baseRoleMapper.select(baseRole);
    }

    @Override
    public Integer qryRoleCountByRoleName(String roleName) {
        BaseRole baseRole = new BaseRole();
        baseRole.setRolename(roleName);
        baseRole.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        return this.baseRoleMapper.selectCount(baseRole);
    }

    @Override
    public Boolean qrySupperByLoginUseruuid() {
        //查询登录用户是否有超级管理员权限
        List<BaseRole> roles = this.qryUserRoleByUuid(this.baseUserService.getShiroUserUuid());
        for (BaseRole bean: roles ) {
            if("3".equals(bean.getUuid())){
                return true;
            }
        }
        return false;
    }

    /**
     * 添加角色菜单中间表
     * @Author zhangYiHang
     * @Date 2018/3/10 22:26
     */
    private void insertRoleMenu(BaseRoleActionBean baseRoleAction){
        if (baseRoleAction.getMenuUuids() != null && baseRoleAction.getMenuUuids().trim().length() != 0){
            String[] menuUuids = baseRoleAction.getMenuUuids().split(",");
            for (String menuUuid : menuUuids){
                BaseRoleMenu roleMenu = new BaseRoleMenu();
                roleMenu.setUuid(UUID.randomUUID().toString());
                roleMenu.setRoleuuid(baseRoleAction.getUuid());
                roleMenu.setMenuuuid(menuUuid);
                this.baseRoleMenuMapper.insert(roleMenu);
            }
        }
    };
}
